from rest_framework.serializers import ModelSerializer
from cart import models
from rest_framework import serializers
from crouse.ser import CourseSerializer
from crouse.models import CourseModel
# class CourseSeria(ModelSerializer):
#     class Meta:
#         model=CourseModel
#         fields=['cname','price','cover',"discount"]
#
# class CartSerializer(ModelSerializer):
#     course = CourseSeria()#嵌套
#     class Meta:
#         model = models.CartModel
#         fields = '__all__'
#
# class CartSer(ModelSerializer):
#     class Meta:
#         model = models.CartModel
#         fields = '__all__'

class CartSerializer(ModelSerializer):
    courselist=serializers.SerializerMethodField(read_only=True)
    def get_courselist(self,obj):
        return {"id":obj.course.id,"cname":obj.course.cname,"price":obj.course.price,"discount":obj.course.discount,"cover":str(obj.course.cover),'level':obj.course.level}
    class Meta:
        model = models.CartModel
        fields = '__all__'